package org.example.cascader.controller;

import org.example.cascader.domain.ZyTreeDTO;
import org.example.cascader.service.ZyTreeService;
import org.example.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/zyTree")
public class ZyTreeController {
    @Autowired
    private ZyTreeService zyTreeService;

    /**
     * 获取完整树形结构
     */
    @GetMapping("/full")
    public Result<List<ZyTreeDTO>> getFullTree() {
        return Result.success(zyTreeService.buildFullTree());
    }

    /**
     * 按层级查询
     */
    @GetMapping("/level/{level}")
    public Result<List<ZyTreeDTO>> getByLevel(@PathVariable int level) {
        if (level < 1 || level > 3) {
            return Result.fail(400, "层级参数错误");
        }
        return Result.success(zyTreeService.getByLevel(level));
    }

    /**
     * 获取子节点
     */
    @GetMapping("/children")
    public Result<List<ZyTreeDTO>> getChildren(
            @RequestParam String parentValue,
            @RequestParam int parentLevel) {
        return Result.success(zyTreeService.getChildren(parentValue, parentLevel));
    }
}
